package styx.lex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class StartingView extends SurfaceView implements SurfaceHolder.Callback {

	public StartingView(Context context) {
		super(context);
		setFocusable(true);
		requestFocusFromTouch();
		setFocusableInTouchMode(true);

		SurfaceHolder holder = this.getHolder();
		holder.addCallback(this);

		thread = new StartingThread(holder, context);
		pathMap = ((CutCutActivity) context)._pathMap;
	}

	private StartingThread thread;
	private Paint cutPaint;
	private int canvasWidth = 1;
	private int canvasHeight = 1;
	private Map<String, ArrayList<PointF>> pathMap;

	class StartingThread extends Thread {
		public Boolean running = false;
		private SurfaceHolder surfaceHolder;
		private Context context;
		private Bitmap backgroundImage;

		public StartingThread(SurfaceHolder surfaceHolder, Context context) {
			this.surfaceHolder = surfaceHolder;
			this.context = context;

			cutPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			cutPaint.setColor(Color.WHITE);
			cutPaint.setStyle(Style.STROKE);
			cutPaint.setStrokeCap(Cap.ROUND);
			cutPaint.setStrokeWidth(2.0f);

			backgroundImage = BitmapFactory.decodeResource(getResources(),
					R.drawable.bg1);
		}

		@Override
		public void run() {
			while (running) {
				synchronized (surfaceHolder) {
					try {
						updateView();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}

		private void updateView() {
			synchronized (pathMap) {
				Canvas c = surfaceHolder.lockCanvas();
				c.drawColor(Color.BLACK);
				try {

					Iterator<Entry<String, ArrayList<PointF>>> iter = pathMap
							.entrySet().iterator();
					while (iter.hasNext()) {
						Entry<String, ArrayList<PointF>> entry = iter.next();
						ArrayList<PointF> path = entry.getValue();
						int size = path.size();
						PointF lastPoint = null;
						for (int i = size; i > 0; i--) {
							PointF pt = path.get(i - 1);
							if (lastPoint != null) {
								c.drawLine(lastPoint.x, lastPoint.y, pt.x,
										pt.y, cutPaint);
							}
							lastPoint = pt;
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					surfaceHolder.unlockCanvasAndPost(c);
				}

			}
		}

		public void setRunning(boolean b) {
			running = b;
		}

		public void setSurfaceSize(int width, int height) {

		}

		public void pause() {

		}
	}

	@Override
	public void onWindowFocusChanged(boolean hasWindowFocus) {
		if (!hasWindowFocus)
			thread.pause();
	}

	// interface implements.
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		synchronized (thread.surfaceHolder) {
			canvasWidth = width;
			canvasHeight = height;

			// TODO: 设置背景图片尺寸
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		thread.setRunning(true);
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		thread.setRunning(false);
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}
	}

	public StartingThread getThread() {
		return thread;
	}

}
